/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdAgregaDetalleActividadMod.java
 *
 * Created on 25-ago-2010, 9:15:04
 */

package Licitaciones;

import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.VerificarDatos;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import suministros.ejb30.TO.LicitacionActividadTO;

/**
 *
 * @author Diana
 */
public class jdAgregaDetalleActividadMod extends javax.swing.JDialog {
    jdAdministrarActividades padre;
    DefaultFormatterFactory currFactory;
    BigDecimal montoTotal;
        /** Creates new form jdAgregaDetalleActividadMod */
    public jdAgregaDetalleActividadMod( jdAdministrarActividades padre,java.awt.Frame parent, boolean modal, BigDecimal montoTotal) {
        super(parent, modal);
        this.padre=padre;
        this.montoTotal=montoTotal;
        construirMascaras();
        initComponents();
    }
     private void construirMascaras() {
    NumberFormat editFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
    editFormat.setGroupingUsed(false);
    NumberFormatter enFormat = new NumberFormatter(editFormat);
    this.currFactory = new DefaultFormatterFactory(enFormat, enFormat, enFormat);
    enFormat.setAllowsInvalid(true);
  }
      private void limpiar() {
        this.jtfCodSuministro.setText("");
        this.jtfDescSuministro.setText("");
        //this.jftfCantidad.setValue(new Double(0.0D));
        //this.jftfPrecio.setValue(new Double(0.0D));
        this.jtfCodSuministro.requestFocus();
        this.jtfTipo.setText("");
        this.jtfUnidad.setText("");

  }
       void setPadreDetalle(jdAdministrarActividades padre){
        this.padre=padre;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jbCancelar = new javax.swing.JButton();
        jpInversion = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jtfCodSuministro = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtfDescSuministro = new javax.swing.JTextField();
        jftfPrecio = new javax.swing.JFormattedTextField();
        this.jftfPrecio.setFormatterFactory(this.currFactory);
        jbAceptar = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtfTipo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfUnidad = new javax.swing.JTextField();
        jftfCantidad = new javax.swing.JFormattedTextField();
        this.jftfPrecio.setFormatterFactory(this.currFactory);

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel13.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel13.setText("PRECIO");

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CERRAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setText("ITEM");

        jtfCodSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfCodSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfCodSuministroActionPerformed(evt);
            }
        });
        jtfCodSuministro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCodSuministroKeyPressed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setText("DESCRIPCION");

        jtfDescSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfDescSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDescSuministroActionPerformed(evt);
            }
        });
        jtfDescSuministro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescSuministroKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jpInversionLayout = new javax.swing.GroupLayout(jpInversion);
        jpInversion.setLayout(jpInversionLayout);
        jpInversionLayout.setHorizontalGroup(
            jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpInversionLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jpInversionLayout.setVerticalGroup(
            jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpInversionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel9)
                .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel10)
                .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jftfPrecio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jftfPrecioActionPerformed(evt);
            }
        });
        jftfPrecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jftfPrecioKeyPressed(evt);
            }
        });

        jbAceptar.setFont(new java.awt.Font("Arial", 0, 12));
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText("ACEPTAR");
        jbAceptar.setToolTipText("Registrar el pedido");
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel12.setText("CANTIDAD");

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("TIPO");

        jtfTipo.setFont(new java.awt.Font("Arial", 0, 12));
        jtfTipo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfTipoKeyPressed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setText("UNIDAD");

        jtfUnidad.setFont(new java.awt.Font("Arial", 0, 12));
        jtfUnidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfUnidadKeyPressed(evt);
            }
        });

        jftfCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jftfCantidadActionPerformed(evt);
            }
        });
        jftfCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jftfCantidadKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(220, 220, 220)
                .addComponent(jbAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCancelar)
                .addGap(36, 36, 36)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jftfPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jtfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(jLabel2)
                .addGap(39, 39, 39)
                .addComponent(jtfUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jLabel12)
                .addGap(18, 18, 18)
                .addComponent(jftfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jpInversion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpInversion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jtfUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jftfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftfPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbAceptar)
                    .addComponent(jbCancelar))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed
      

}//GEN-LAST:event_jbCancelarKeyPressed

    private void jtfCodSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfCodSuministroActionPerformed


}//GEN-LAST:event_jtfCodSuministroActionPerformed

    private void jtfDescSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDescSuministroActionPerformed

}//GEN-LAST:event_jtfDescSuministroActionPerformed

    private void jtfDescSuministroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescSuministroKeyPressed
        
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jtfTipo.requestFocus();
        }
}//GEN-LAST:event_jtfDescSuministroKeyPressed

    private void jftfPrecioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jftfPrecioActionPerformed
       
}//GEN-LAST:event_jftfPrecioActionPerformed

    private void jftfPrecioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jftfPrecioKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.requestFocus();
        }
}//GEN-LAST:event_jftfPrecioKeyPressed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        

        LicitacionActividadTO detalle = new LicitacionActividadTO();
        try {
            /*float cantidad = VerificarDatos.verificaFloatSoloPositivo(this.jftfCantidad.getValue());
            if(cantidad==0 ){
                Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + "La cantidad debe ser mayor a 0");
                return;
            }
            float precio = VerificarDatos.verificaFloatSoloPositivo(this.jftfPrecio.getValue());
            if(precio==0 ){
                Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + "El precio debe ser mayor a 0");
                return;
            }*/
            detalle.setCodigo_item(this.jtfCodSuministro.getText());
            detalle.setDescripcion_actividad(this.jtfDescSuministro.getText());
            detalle.setUnidad(this.jtfUnidad.getText());
            detalle.setId_tipo_actividad(Integer.parseInt(this.jtfTipo.getText()));
            detalle.setCantidad_disponible(new BigDecimal(this.jftfCantidad.getText()));
            detalle.setCantidadAdquiridaActual(BigDecimal.ZERO);
            detalle.setCantidad(new BigDecimal(this.jftfCantidad.getText()));
            detalle.setCosto_unitario(new BigDecimal(this.jftfPrecio.getText()));
            detalle.setMonto_adquirido(BigDecimal.ZERO);
            detalle.setCantidadAdquiridaActual(BigDecimal.ZERO);


            BigDecimal suma=BigDecimal.ZERO;
            //verificar montos no sobrepasan
             for(int i=0; i<padre.actividades.size(); i++){
                BigDecimal monto=padre.actividades.get(i).getCantidad().multiply(padre.actividades.get(i).getCosto_unitario());
                suma=suma.add(monto);
             }
             suma=suma.add(detalle.getCantidad().multiply(detalle.getCosto_unitario()));

             if((this.montoTotal).compareTo(suma)<0){
                     int res1 = javax.swing.JOptionPane.showConfirmDialog( this,"El monto total '"+montoTotal+"' de las actividades sobrepasa al registrado  \n ¿Esta seguro de ingresar la nueva actividad?",
                    "Confirmar",javax.swing.JOptionPane.YES_NO_OPTION );
                     if(res1==javax.swing.JOptionPane.YES_OPTION){

                        this.padre.agregaActividad(detalle);
                        limpiar();
                        dispose();
                     }
             }
            limpiar();
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + e.getMessage());
        }
}//GEN-LAST:event_jbAceptarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.doClick();
        }
}//GEN-LAST:event_jbAceptarKeyPressed

    private void jtfCodSuministroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodSuministroKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jtfDescSuministro.requestFocus();
        }
    }//GEN-LAST:event_jtfCodSuministroKeyPressed

    private void jtfTipoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTipoKeyPressed
          if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jtfUnidad.requestFocus();
        }
    }//GEN-LAST:event_jtfTipoKeyPressed

    private void jtfUnidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfUnidadKeyPressed
         if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jftfCantidad.requestFocus();
        }
    }//GEN-LAST:event_jtfUnidadKeyPressed

    private void jftfCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jftfCantidadActionPerformed
       
    }//GEN-LAST:event_jftfCantidadActionPerformed

    private void jftfCantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jftfCantidadKeyPressed
        
    }//GEN-LAST:event_jftfCantidadKeyPressed

    /**
    * @param args the command line arguments
    */
 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JFormattedTextField jftfCantidad;
    private javax.swing.JFormattedTextField jftfPrecio;
    private javax.swing.JPanel jpInversion;
    private javax.swing.JTextField jtfCodSuministro;
    private javax.swing.JTextField jtfDescSuministro;
    private javax.swing.JTextField jtfTipo;
    private javax.swing.JTextField jtfUnidad;
    // End of variables declaration//GEN-END:variables

}
